library(tidyverse)
## ── Attaching packages ────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.0     ✔ purrr   0.3.2
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   0.8.3     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.3.0
## ── Conflicts ───────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(gganimate) 
library(gapminder)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
data("gapminder")
glimpse(gapminder)
## Observations: 1,704
## Variables: 6
## $ country   <fct> Afghanistan, Afghanistan, Afghanistan, Afghanistan, Af…
## $ continent <fct> Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, …
## $ year      <int> 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992, …
## $ lifeExp   <dbl> 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.854…
## $ pop       <int> 8425333, 9240934, 10267083, 11537966, 13079460, 148803…
## $ gdpPercap <dbl> 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 786.…
hist(gapminder$year)

hist(gapminder$lifeExp)

hist(gapminder$pop,breaks=100)

hist(gapminder$gdpPercap,breaks = 50)

table(gapminder$country,gapminder$continent)
##                           
##                            Africa Americas Asia Europe Oceania
##   Afghanistan                   0        0   12      0       0
##   Albania                       0        0    0     12       0
##   Algeria                      12        0    0      0       0
##   Angola                       12        0    0      0       0
##   Argentina                     0       12    0      0       0
##   Australia                     0        0    0      0      12
##   Austria                       0        0    0     12       0
##   Bahrain                       0        0   12      0       0
##   Bangladesh                    0        0   12      0       0
##   Belgium                       0        0    0     12       0
##   Benin                        12        0    0      0       0
##   Bolivia                       0       12    0      0       0
##   Bosnia and Herzegovina        0        0    0     12       0
##   Botswana                     12        0    0      0       0
##   Brazil                        0       12    0      0       0
##   Bulgaria                      0        0    0     12       0
##   Burkina Faso                 12        0    0      0       0
##   Burundi                      12        0    0      0       0
##   Cambodia                      0        0   12      0       0
##   Cameroon                     12        0    0      0       0
##   Canada                        0       12    0      0       0
##   Central African Republic     12        0    0      0       0
##   Chad                         12        0    0      0       0
##   Chile                         0       12    0      0       0
##   China                         0        0   12      0       0
##   Colombia                      0       12    0      0       0
##   Comoros                      12        0    0      0       0
##   Congo, Dem. Rep.             12        0    0      0       0
##   Congo, Rep.                  12        0    0      0       0
##   Costa Rica                    0       12    0      0       0
##   Cote d'Ivoire                12        0    0      0       0
##   Croatia                       0        0    0     12       0
##   Cuba                          0       12    0      0       0
##   Czech Republic                0        0    0     12       0
##   Denmark                       0        0    0     12       0
##   Djibouti                     12        0    0      0       0
##   Dominican Republic            0       12    0      0       0
##   Ecuador                       0       12    0      0       0
##   Egypt                        12        0    0      0       0
##   El Salvador                   0       12    0      0       0
##   Equatorial Guinea            12        0    0      0       0
##   Eritrea                      12        0    0      0       0
##   Ethiopia                     12        0    0      0       0
##   Finland                       0        0    0     12       0
##   France                        0        0    0     12       0
##   Gabon                        12        0    0      0       0
##   Gambia                       12        0    0      0       0
##   Germany                       0        0    0     12       0
##   Ghana                        12        0    0      0       0
##   Greece                        0        0    0     12       0
##   Guatemala                     0       12    0      0       0
##   Guinea                       12        0    0      0       0
##   Guinea-Bissau                12        0    0      0       0
##   Haiti                         0       12    0      0       0
##   Honduras                      0       12    0      0       0
##   Hong Kong, China              0        0   12      0       0
##   Hungary                       0        0    0     12       0
##   Iceland                       0        0    0     12       0
##   India                         0        0   12      0       0
##   Indonesia                     0        0   12      0       0
##   Iran                          0        0   12      0       0
##   Iraq                          0        0   12      0       0
##   Ireland                       0        0    0     12       0
##   Israel                        0        0   12      0       0
##   Italy                         0        0    0     12       0
##   Jamaica                       0       12    0      0       0
##   Japan                         0        0   12      0       0
##   Jordan                        0        0   12      0       0
##   Kenya                        12        0    0      0       0
##   Korea, Dem. Rep.              0        0   12      0       0
##   Korea, Rep.                   0        0   12      0       0
##   Kuwait                        0        0   12      0       0
##   Lebanon                       0        0   12      0       0
##   Lesotho                      12        0    0      0       0
##   Liberia                      12        0    0      0       0
##   Libya                        12        0    0      0       0
##   Madagascar                   12        0    0      0       0
##   Malawi                       12        0    0      0       0
##   Malaysia                      0        0   12      0       0
##   Mali                         12        0    0      0       0
##   Mauritania                   12        0    0      0       0
##   Mauritius                    12        0    0      0       0
##   Mexico                        0       12    0      0       0
##   Mongolia                      0        0   12      0       0
##   Montenegro                    0        0    0     12       0
##   Morocco                      12        0    0      0       0
##   Mozambique                   12        0    0      0       0
##   Myanmar                       0        0   12      0       0
##   Namibia                      12        0    0      0       0
##   Nepal                         0        0   12      0       0
##   Netherlands                   0        0    0     12       0
##   New Zealand                   0        0    0      0      12
##   Nicaragua                     0       12    0      0       0
##   Niger                        12        0    0      0       0
##   Nigeria                      12        0    0      0       0
##   Norway                        0        0    0     12       0
##   Oman                          0        0   12      0       0
##   Pakistan                      0        0   12      0       0
##   Panama                        0       12    0      0       0
##   Paraguay                      0       12    0      0       0
##   Peru                          0       12    0      0       0
##   Philippines                   0        0   12      0       0
##   Poland                        0        0    0     12       0
##   Portugal                      0        0    0     12       0
##   Puerto Rico                   0       12    0      0       0
##   Reunion                      12        0    0      0       0
##   Romania                       0        0    0     12       0
##   Rwanda                       12        0    0      0       0
##   Sao Tome and Principe        12        0    0      0       0
##   Saudi Arabia                  0        0   12      0       0
##   Senegal                      12        0    0      0       0
##   Serbia                        0        0    0     12       0
##   Sierra Leone                 12        0    0      0       0
##   Singapore                     0        0   12      0       0
##   Slovak Republic               0        0    0     12       0
##   Slovenia                      0        0    0     12       0
##   Somalia                      12        0    0      0       0
##   South Africa                 12        0    0      0       0
##   Spain                         0        0    0     12       0
##   Sri Lanka                     0        0   12      0       0
##   Sudan                        12        0    0      0       0
##   Swaziland                    12        0    0      0       0
##   Sweden                        0        0    0     12       0
##   Switzerland                   0        0    0     12       0
##   Syria                         0        0   12      0       0
##   Taiwan                        0        0   12      0       0
##   Tanzania                     12        0    0      0       0
##   Thailand                      0        0   12      0       0
##   Togo                         12        0    0      0       0
##   Trinidad and Tobago           0       12    0      0       0
##   Tunisia                      12        0    0      0       0
##   Turkey                        0        0    0     12       0
##   Uganda                       12        0    0      0       0
##   United Kingdom                0        0    0     12       0
##   United States                 0       12    0      0       0
##   Uruguay                       0       12    0      0       0
##   Venezuela                     0       12    0      0       0
##   Vietnam                       0        0   12      0       0
##   West Bank and Gaza            0        0   12      0       0
##   Yemen, Rep.                   0        0   12      0       0
##   Zambia                       12        0    0      0       0
##   Zimbabwe                     12        0    0      0       0
xtabs(~ gapminder$continent + gapminder$country)
##                    gapminder$country
## gapminder$continent Afghanistan Albania Algeria Angola Argentina Australia
##            Africa             0       0      12     12         0         0
##            Americas           0       0       0      0        12         0
##            Asia              12       0       0      0         0         0
##            Europe             0      12       0      0         0         0
##            Oceania            0       0       0      0         0        12
##                    gapminder$country
## gapminder$continent Austria Bahrain Bangladesh Belgium Benin Bolivia
##            Africa         0       0          0       0    12       0
##            Americas       0       0          0       0     0      12
##            Asia           0      12         12       0     0       0
##            Europe        12       0          0      12     0       0
##            Oceania        0       0          0       0     0       0
##                    gapminder$country
## gapminder$continent Bosnia and Herzegovina Botswana Brazil Bulgaria
##            Africa                        0       12      0        0
##            Americas                      0        0     12        0
##            Asia                          0        0      0        0
##            Europe                       12        0      0       12
##            Oceania                       0        0      0        0
##                    gapminder$country
## gapminder$continent Burkina Faso Burundi Cambodia Cameroon Canada
##            Africa             12      12        0       12      0
##            Americas            0       0        0        0     12
##            Asia                0       0       12        0      0
##            Europe              0       0        0        0      0
##            Oceania             0       0        0        0      0
##                    gapminder$country
## gapminder$continent Central African Republic Chad Chile China Colombia
##            Africa                         12   12     0     0        0
##            Americas                        0    0    12     0       12
##            Asia                            0    0     0    12        0
##            Europe                          0    0     0     0        0
##            Oceania                         0    0     0     0        0
##                    gapminder$country
## gapminder$continent Comoros Congo, Dem. Rep. Congo, Rep. Costa Rica
##            Africa        12               12          12          0
##            Americas       0                0           0         12
##            Asia           0                0           0          0
##            Europe         0                0           0          0
##            Oceania        0                0           0          0
##                    gapminder$country
## gapminder$continent Cote d'Ivoire Croatia Cuba Czech Republic Denmark
##            Africa              12       0    0              0       0
##            Americas             0       0   12              0       0
##            Asia                 0       0    0              0       0
##            Europe               0      12    0             12      12
##            Oceania              0       0    0              0       0
##                    gapminder$country
## gapminder$continent Djibouti Dominican Republic Ecuador Egypt El Salvador
##            Africa         12                  0       0    12           0
##            Americas        0                 12      12     0          12
##            Asia            0                  0       0     0           0
##            Europe          0                  0       0     0           0
##            Oceania         0                  0       0     0           0
##                    gapminder$country
## gapminder$continent Equatorial Guinea Eritrea Ethiopia Finland France
##            Africa                  12      12       12       0      0
##            Americas                 0       0        0       0      0
##            Asia                     0       0        0       0      0
##            Europe                   0       0        0      12     12
##            Oceania                  0       0        0       0      0
##                    gapminder$country
## gapminder$continent Gabon Gambia Germany Ghana Greece Guatemala Guinea
##            Africa      12     12       0    12      0         0     12
##            Americas     0      0       0     0      0        12      0
##            Asia         0      0       0     0      0         0      0
##            Europe       0      0      12     0     12         0      0
##            Oceania      0      0       0     0      0         0      0
##                    gapminder$country
## gapminder$continent Guinea-Bissau Haiti Honduras Hong Kong, China Hungary
##            Africa              12     0        0                0       0
##            Americas             0    12       12                0       0
##            Asia                 0     0        0               12       0
##            Europe               0     0        0                0      12
##            Oceania              0     0        0                0       0
##                    gapminder$country
## gapminder$continent Iceland India Indonesia Iran Iraq Ireland Israel Italy
##            Africa         0     0         0    0    0       0      0     0
##            Americas       0     0         0    0    0       0      0     0
##            Asia           0    12        12   12   12       0     12     0
##            Europe        12     0         0    0    0      12      0    12
##            Oceania        0     0         0    0    0       0      0     0
##                    gapminder$country
## gapminder$continent Jamaica Japan Jordan Kenya Korea, Dem. Rep.
##            Africa         0     0      0    12                0
##            Americas      12     0      0     0                0
##            Asia           0    12     12     0               12
##            Europe         0     0      0     0                0
##            Oceania        0     0      0     0                0
##                    gapminder$country
## gapminder$continent Korea, Rep. Kuwait Lebanon Lesotho Liberia Libya
##            Africa             0      0       0      12      12    12
##            Americas           0      0       0       0       0     0
##            Asia              12     12      12       0       0     0
##            Europe             0      0       0       0       0     0
##            Oceania            0      0       0       0       0     0
##                    gapminder$country
## gapminder$continent Madagascar Malawi Malaysia Mali Mauritania Mauritius
##            Africa           12     12        0   12         12        12
##            Americas          0      0        0    0          0         0
##            Asia              0      0       12    0          0         0
##            Europe            0      0        0    0          0         0
##            Oceania           0      0        0    0          0         0
##                    gapminder$country
## gapminder$continent Mexico Mongolia Montenegro Morocco Mozambique Myanmar
##            Africa        0        0          0      12         12       0
##            Americas     12        0          0       0          0       0
##            Asia          0       12          0       0          0      12
##            Europe        0        0         12       0          0       0
##            Oceania       0        0          0       0          0       0
##                    gapminder$country
## gapminder$continent Namibia Nepal Netherlands New Zealand Nicaragua Niger
##            Africa        12     0           0           0         0    12
##            Americas       0     0           0           0        12     0
##            Asia           0    12           0           0         0     0
##            Europe         0     0          12           0         0     0
##            Oceania        0     0           0          12         0     0
##                    gapminder$country
## gapminder$continent Nigeria Norway Oman Pakistan Panama Paraguay Peru
##            Africa        12      0    0        0      0        0    0
##            Americas       0      0    0        0     12       12   12
##            Asia           0      0   12       12      0        0    0
##            Europe         0     12    0        0      0        0    0
##            Oceania        0      0    0        0      0        0    0
##                    gapminder$country
## gapminder$continent Philippines Poland Portugal Puerto Rico Reunion
##            Africa             0      0        0           0      12
##            Americas           0      0        0          12       0
##            Asia              12      0        0           0       0
##            Europe             0     12       12           0       0
##            Oceania            0      0        0           0       0
##                    gapminder$country
## gapminder$continent Romania Rwanda Sao Tome and Principe Saudi Arabia
##            Africa         0     12                    12            0
##            Americas       0      0                     0            0
##            Asia           0      0                     0           12
##            Europe        12      0                     0            0
##            Oceania        0      0                     0            0
##                    gapminder$country
## gapminder$continent Senegal Serbia Sierra Leone Singapore Slovak Republic
##            Africa        12      0           12         0               0
##            Americas       0      0            0         0               0
##            Asia           0      0            0        12               0
##            Europe         0     12            0         0              12
##            Oceania        0      0            0         0               0
##                    gapminder$country
## gapminder$continent Slovenia Somalia South Africa Spain Sri Lanka Sudan
##            Africa          0      12           12     0         0    12
##            Americas        0       0            0     0         0     0
##            Asia            0       0            0     0        12     0
##            Europe         12       0            0    12         0     0
##            Oceania         0       0            0     0         0     0
##                    gapminder$country
## gapminder$continent Swaziland Sweden Switzerland Syria Taiwan Tanzania
##            Africa          12      0           0     0      0       12
##            Americas         0      0           0     0      0        0
##            Asia             0      0           0    12     12        0
##            Europe           0     12          12     0      0        0
##            Oceania          0      0           0     0      0        0
##                    gapminder$country
## gapminder$continent Thailand Togo Trinidad and Tobago Tunisia Turkey
##            Africa          0   12                   0      12      0
##            Americas        0    0                  12       0      0
##            Asia           12    0                   0       0      0
##            Europe          0    0                   0       0     12
##            Oceania         0    0                   0       0      0
##                    gapminder$country
## gapminder$continent Uganda United Kingdom United States Uruguay Venezuela
##            Africa       12              0             0       0         0
##            Americas      0              0            12      12        12
##            Asia          0              0             0       0         0
##            Europe        0             12             0       0         0
##            Oceania       0              0             0       0         0
##                    gapminder$country
## gapminder$continent Vietnam West Bank and Gaza Yemen, Rep. Zambia Zimbabwe
##            Africa         0                  0           0     12       12
##            Americas       0                  0           0      0        0
##            Asia          12                 12          12      0        0
##            Europe         0                  0           0      0        0
##            Oceania        0                  0           0      0        0
uniqcountries <- function(x){gapminder %>%
  filter(continent == x) %>%
  select(country) %>% unique()
}



names(gapminder)
## [1] "country"   "continent" "year"      "lifeExp"   "pop"       "gdpPercap"
ggplot(gapminder,aes(x=country,y=lifeExp)) +
  geom_boxplot() + facet_wrap(~continent)

ggplot(gapminder,aes(x=year,y=lifeExp)) +
  geom_point() + facet_wrap(~continent) +
  geom_smooth(se=FALSE,aes(color=country),show.legend = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

# africa by itself

fig1 <- gapminder %>% filter(continent == "Africa") %>%
  ggplot(aes(x=year,y=lifeExp)) +
  geom_point() + 
  geom_smooth(se=FALSE,aes(color=country),show.legend = TRUE)

# neato!
ggplotly(fig1)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# oceania

fig2 <- gapminder %>% filter(continent == "Oceania") %>%
  ggplot(aes(x=year,y=lifeExp)) +
  geom_point() + 
  geom_smooth(se=FALSE,aes(color=country),show.legend = TRUE)

fig2 + lims(y=c(0,100))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplotly(fig2)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
p1 = ggplot(gapminder, aes(x=year,y=lifeExp,color=continent)) +
  geom_smooth(se=FALSE)


p2 = ggplot(gapminder, aes(x=gdpPercap,y=lifeExp,color=continent,size=pop)) +
  geom_point()

gridExtra::grid.arrange(p1,p2)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot(gapminder, aes(y=gdpPercap,x=year,color=continent)) +
  geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p2 + facet_wrap(~year) + scale_x_log10()

ggplot(gapminder,aes(x=year,y=pop,color=continent)) +
  geom_point() + geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p3 = gapminder %>% filter(continent=="Asia") %>%
  ggplot(aes(x=year,y=pop,color=country)) +
  geom_point() + geom_smooth(se=FALSE)

# neato zoom!
ggplotly(p3)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
ggplot(gapminder, aes(x=gdpPercap, y=lifeExp, size = pop, colour = country)) +
  geom_point(alpha = 0.7, show.legend = FALSE) +
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  facet_wrap(~continent) + labs(subtitle = "Points scaled to country population") +

  # Here comes the gganimate specific bits
  labs(title = 'Year: {frame_time}', x = 'GDP per capita', y = 'life expectancy') +
  transition_time(year) +
  ease_aes('linear')